<template>
  <div class="permission-container">
    <div class="app-container">
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>权限管理</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button type="primary" size="small" @click="addPermission(1, '0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-card style="margin-top: 10px">
        <el-table border :data="permissionList" stripe row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="addPermission(2, row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <PerDialog ref="perDialogRef" :show-dialog.sync="showDialog" :init-permission-list="initPermissionList" :is-edit="isEdit" @confirmEV="confirmFn" />
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, transTree, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import PerDialog from './components/perDialog.vue'

export default {
  name: 'Permission',
  components: { PerDialog },
  data() {
    return {
      permissionList: [],
      showDialog: false,
      isEdit: false,
      initPermissionList: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListAPI()
      this.initPermissionList = res.data
      this.permissionList = transTree(res.data, '0')
    },
    addPermission(type, pid) {
      this.isEdit = false
      this.showDialog = true
      this.$refs.perDialogRef.formData.type = type
      this.$refs.perDialogRef.formData.pid = pid
    },
    // 确认提交
    async confirmFn(data) {
      let res = null
      if (this.isEdit) {
        res = await updatePermissionAPI(data)
      } else {
        res = await addPermissionAPI(data)
      }
      if (res.success) {
        this.$message.success(res.message)
      } else {
        this.$message.error(res.message)
      }
      this.getPermissionList()
    },
    // 编辑权限
    async editPermission(id) {
      this.isEdit = true
      this.showDialog = true
      const res = await getPermissionDetailAPI(id)
      // 数据回显
      this.$refs.perDialogRef.formData = res.data
    },

    // 删除权限
    async delPermission(id) {
      this.$confirm('确定删除该权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermissionAPI(id)
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      }).finally(() => {
        this.getPermissionList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
